package com.example.java_gobang.tests;

import com.example.java_gobang.common.BrowserName;
import com.example.java_gobang.common.Utils;
import lombok.SneakyThrows;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.UUID;

/**
 * @author hanzishuai
 * Blog :  https://blog.csdn.net/qrwitu142857
 * @date 2025/5/11 22:16
 * @Description
 */
public class RegisterPage extends Utils {
    public static final String url = "http://101.201.246.67:58080/register.html";

    public RegisterPage() {
        super(url);
    }

    public RegisterPage(BrowserName browserName) {
        super(url, browserName);
    }

    /**
     * Created by hanzishuai on 2025/5/11
     *
     * @Description 检查注册页面是否加载完毕
     */
    @SneakyThrows
    public void checkRegisterPageRight() {
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        screenShot("checkRegisterPageRight");
    }


    private void register(String username, String password) {
        // 将输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        // 输入账号密码，并点击提交
        if (username != null) {
            driver.findElement(By.cssSelector("#username")).sendKeys(username);
        }
        if (password != null) {
            driver.findElement(By.cssSelector("#password")).sendKeys(password);
        }

        driver.findElement(By.cssSelector("#submit")).click();
    }


    @SneakyThrows
    public void registerFail() {
        // 不输入账号和密码，点击提交
        register(null, null);
        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        // 处理弹窗
        Alert alert = driver.switchTo().alert();
        String text = alert.getText();
        assert text.equals("注册失败！");

        // 点击确定
        alert.accept();

        // 截图
        screenShot("registerFail");

        // 输入账号，不输入密码，点击提交
        register("awa1", null);
        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        // 处理弹窗
        alert = driver.switchTo().alert();
        text = alert.getText();
        assert text.equals("注册失败！");

        // 点击确定
        alert.accept();

        // 截图
        screenShot("registerFail");

        // 不输入账号，输入密码，点击提交
        register(null, "123");
        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        // 处理弹窗
        alert = driver.switchTo().alert();
        text = alert.getText();
        assert text.equals("注册失败！");

        // 点击确定
        alert.accept();

        // 截图
        screenShot("registerFail");

        // 输入已注册的账号，输入正确的密码，点击提交
        register("awa", "123456");
        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        // 处理弹窗
        alert = driver.switchTo().alert();
        text = alert.getText();
        assert text.equals("注册失败！");

        // 点击确定
        alert.accept();

        // 截图
        screenShot("registerFail");
    }

    @SneakyThrows
    public void registerSuccess() {
        String username = UUID.randomUUID().toString().replace("-", "").substring(0,7);
        register(username, "123");
        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        // 处理弹窗
        Alert alert = driver.switchTo().alert();
        String text = alert.getText();
        assert text.equals("注册成功！");

        // 点击确定
        alert.accept();

        // 截图
        screenShot("registerSuccess");

        // 注册成功会跳转到登录页面
        String text1 = driver.findElement(By.cssSelector("body > div.login-container > div.login-dialog > h3")).getText();

        assert  "登录".equals(text1);
    }


}
